#!/bin/sh

if [ -n "$_VL_IMPORT_STEP_BUILD" ] ; then
	return 0;
fi
_VL_IMPORT_STEP_BUILD=0

# 加载项目环境
if ! load "$VL_ENV" ; then
	return 1;
fi


# 判断是否需要重新 Build
# 如果需要则直接进行 build
#
# 返回值 : 构建成功返回 0
function checkBuild() {

	if ! isVLStyleProject ; then
		printWarn "Not a vl-style project ... ignore build"
		return 0
	fi

	if checkFileExpire "$VL_CACHE_FILE_CONFIG_TS" "$VL_FILE_CONFIG" || checkVariable "$VL_ARG_FORCE_BUILD" ; then
		# 加载依赖构建脚本
		if ! load $VL_ENV_PART_STEP_FOR_DEP ; then
			return 1
		fi

		# 检查项目配置文件更新，重新构建
		echo "build ..."
		build
		return $?
	fi

	if checkFileExpire "$VL_CACHE_FILE_DEP_TS" "$VL_FILE_DEP" ; then
		# 加载依赖构建脚本
		if ! load $VL_ENV_PART_STEP_FOR_DEP ; then
			return 1
		fi

		if ! checkDependencies ; then
			return 1
		fi

		return 0
	fi

	return 0
}

# 构建项目结构
#
# 返回值 : 构建成功返回 0
function build() {
	case "$VL_TYPE" in
	exec)
		# build for executable
		echo "build for executable"

		if ! buildProject || ! buildExec ; then
			return 1
		fi
	;;
	lib)
		# build for library
		echo "build for library"

		if ! buildProject || ! buildLibrary ; then
			return 1
		fi
	;;
	*)
		printError "unsupported project type: $VL_TYPE"
		return 1
	;;
	esac

	
	# todo 编译工具链处理


	# 生成配置文件时间戳记录文件
	if ! saveFileTs "$VL_CACHE_FILE_CONFIG_TS" "$VL_FILE_CONFIG" ; then
		printError "create \"$VL_CACHE_FILE_CONFIG_TS\" failed"
		return 1
	fi

	return 0
}
# 构建基本项目结构
function buildProject() {
	# 加载构建生成脚本
	if ! load "$VL_ENV_PART_GENERATE_FOR_BUILD" ; then
		return 1
	fi

	# .vl-build
	if ! checkDirOrCreate "$VL_DIR_BUILD" ; then
		printError "create \"$VL_DIR_BUILD\" dir failed"
		return 1
	fi

	# 先处理依赖
	if ! checkDependencies ; then
		return 1
	fi

	# 进入项目根目录, 创建一些 autotools 项目所需的一些必要文件
	cd $VL_DIR_ROOT

	#
	# 创建 automake 项目结构
	#

	# src 源码目录
	if ! checkDirOrCreate "src" ; then
		printError "create \"src\" dir failed"
		return 1
	fi

	# Makefile.am
	if ! generateProjectMakefile ; then
		printError "create \"Makefile.am\" failed"
		return 1
	fi

	# AUTHORS
	if ! checkFileOrCreate "AUTHORS" ; then
		printError "create \"AUTHORS\" failed"
		return 1
	fi

	# ChangeLog
	if ! checkFileOrCreate "ChangeLog" ; then
		printError "create \"ChangeLog\" failed"
		return 1
	fi

	# NEWS
	if ! checkFileOrCreate "NEWS" ; then
		printError "create \"NEWS\" failed"
		return 1
	fi

	# README
	if ! checkFileOrCreate "README" ; then
		printError "create \"README\" failed"
		return 1
	fi

	# .gitignore
	if ! generateGitIgnore ; then
		printError "create \"README\" failed"
		return 1
	fi

	# vl_compile_options.sh 编译参数
	if ! generateCompileOptions ; then
		printError "create \"vl_compile_options.sh\" failed"
		return 1
	fi

}

# 构建可执行文件项目结构
function buildExec() {
	cd $VL_DIR_ROOT

	# configure.ac
	if ! generateExecConfigureAc ; then
		printError "create \"configure.ac\" failed"
		return 1
	fi

	cd $VL_DIR_SRC

	# 源文件
	if ! generateExecSource ; then
		printError "create \"src/${VL_NAME}.cpp\" failed"
		return 1
	fi

	# Makefile.am
	if ! generateExecMakefile ; then
		printError "create \"src/Makefile.am\" failed"
		return 1
	fi

	return 0
}

# 构建库文件项目结构
function buildLibrary() {
	cd $VL_DIR_ROOT

	# configure.ac
	if ! generateLibraryConfigureAc ; then
		printError "create \"configure.ac\" failed"
		return 1
	fi

	cd $VL_DIR_SRC
	
	# 头文件
	if ! checkFileOrCreate "${VL_NAME}.h" ; then
		printError "create \"${VL_NAME}.h\" failed"
		return 1
	fi

	# 源文件
	if ! checkFileOrCreate "${VL_NAME}.cpp" ; then
		printError "create \"${VL_NAME}.cpp\" failed"
		return 1
	fi

	# Makefile.am
	if ! generateLibraryMakefile ; then
		printError "create \"src/Makefile.am\" failed"
		return 1
	fi

	# test dir
	if ! checkDirOrCreate "test" ; then
		printError "create \"src/test\" dir failed"
		return 1
	fi

	cd test

	# test.cpp
	if ! generateTestProgram ; then
		printError "create \"src/test/test.cpp\" dir failed"
		return 1
	fi

	return 0
}